import numpy as np

class MinAvgGrouper:
    def __init__(self,second_list:list[int],truncate:int=0):
        if len(second_list)==0: raise RuntimeError("秒列表为空")
        self.__truncpos = 0
        while second_list[self.__truncpos]<truncate:
            self.__truncpos += 1
            if self.__truncpos>=len(second_list): break
        last_sec = second_list[-1]
        self.__second_list = second_list[self.__truncpos:]
        if len(self.__second_list)==0: raise RuntimeError(f"MinAvgGrouper的起始时间({truncate})超过了数据记录的时间{last_sec}")
        self.__mapper = [s//60 for s in self.__second_list]
        tmp=set()
        self.__minlist:list[int] = []
        for v in self.__mapper:
            if not v in tmp:
                self.__minlist.append(v)
                tmp.add(v)
        self.__time_line:list[str] = []
        for i in range(self.__minlist[0],self.__minlist[-1]+1):
            h, m = divmod(i, 60)
            d, h = divmod(h, 24)
            self.__time_line.append(f"第{d}天 {h:02}:{m:02}")
    
    @property
    def TruncatePos(self)->int: return self.__truncpos
    
    @property
    def SecondList(self)->list[int]: return self.__second_list

    @property
    def MinuteList(self)->list[int]: return self.__minlist

    @property
    def TimeLine(self)->list[str]: return self.__time_line

    def Process(self,values:np.ndarray)->list:
        values = values[self.__truncpos:]
        if len(self.__mapper)!=len(values): raise ValueError(f"时间轴长度{len(self.__mapper)}和数据数量{len(values)}不匹配")
        bucket:dict[int,list]={}
        for m,v in zip(self.__mapper,values):
            if not m in bucket:
                bucket[m]=[1,v]
            else:
                bucket[m][0]+=1
                bucket[m][1]+=v
        return [bucket[m][1]/bucket[m][0] for m in self.__minlist]